package util.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import util.enumeration.CuisineType;
import util.enumeration.Language;
import util.enumhelper.SupportedLocale;
import util.localization.WebLocalization;

@FacesConverter(value = "cuisineTypeConverter", forClass = util.enumeration.CuisineType.class)
public class CuisineTypeConverter implements Converter {
    private WebLocalization localization;
    private String preferredLocale;
    
    public CuisineTypeConverter() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        /* END LOCALIZATION */
    }
    
    //getAsObject method converts a string to a an object
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value == null || value.length() == 0) {
            return null;
        }
        
        /* NOTE:
         * currently, the checkbox list actually returns all the same string for the object part
         * but, I just split them into diff locales just in case we need them one day.
         */
        
        // en_US ENGLISH, UNITED STATES
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
            return getCuisineTypeAsObject_EN_US(value);
        }
        // en_SG ENGLISH, SINGAPORE
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
            return getCuisineTypeAsObject_EN_SG(value);
        }
        // ms_MY MALAY, MALAYSIA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
            return getCuisineTypeAsObject_MS_MY(value);
        }

        // zh_CN CHINESE, CHINA
        else
        if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
            return getCuisineTypeAsObject_ZH_CN(value);
        }
        // other language...
        else {
            return getCuisineTypeAsObject_EN_US(value);
        }
    }
    
    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        
        preferredLocale = localization.getPreferredLocaleFromSession();
        
        if (value == null) {
            return null;
        }
        
        if (value instanceof CuisineType) {
            CuisineType cuisineType = (CuisineType) value;
            
            String localizedCuisineType = "";
            
            // en_US ENGLISH, UNITED STATES
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_US))) {
                localizedCuisineType = getCuisineTypeAsString_EN_US(cuisineType);
            }
            // en_SG ENGLISH, SINGAPORE
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.ENGLISH_SINGAPORE))) {
                localizedCuisineType = getCuisineTypeAsString_EN_SG(cuisineType);
            }
            // ms_MY MALAY, MALAYSIA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.MALAY))) {
                localizedCuisineType = getCuisineTypeAsString_MS_MY(cuisineType);
            }

            // zh_CN CHINESE, CHINA
            else
            if (preferredLocale.equals(SupportedLocale.getSupportedLocaleStringByLanguage(Language.CHINESE_CHINA))) {
                localizedCuisineType = getCuisineTypeAsString_ZH_CN(cuisineType);
            }
            // other language...
            else {
                localizedCuisineType = getCuisineTypeAsString_EN_US(cuisineType);
            }

            return localizedCuisineType;
        }
        else {
            throw new IllegalArgumentException("object " + value + " is of type " + value.getClass().getName() + "; expected type: util.enumeration.CuisineType");
        }
    }
    
    // simplified chinese (zh_CN)
    private static Object getCuisineTypeAsObject_ZH_CN(String value) {
        if(value.equals("中国菜")) return CuisineType.CHINESE;
        else if(value.equals("粤菜")) return CuisineType.CANTONESE;
        else if(value.equals("海南美食")) return CuisineType.HAINANESE;
        else if(value.equals("客家菜")) return CuisineType.HAKKA;
        else if(value.equals("福建美食")) return CuisineType.HOKKIEN;
        else if(value.equals("闽菜")) return CuisineType.FUJIAN;
        else if(value.equals("上海菜")) return CuisineType.SHANGHAI;
        else if(value.equals("潮州美食")) return CuisineType.TEOCHEW;
        else if(value.equals("新疆美食")) return CuisineType.XINJIANG;
        else if(value.equals("马来美食")) return CuisineType.MALAY;
        else if(value.equals("印度美食")) return CuisineType.INDIAN;
        else if(value.equals("中西美食")) return CuisineType.WESTERN;
        else if(value.equals("日本料理")) return CuisineType.JAPANESE;
        else if(value.equals("韩国料理")) return CuisineType.KOREAN;
        else return null;
    }
    
    // default (en_US)
    private static Object getCuisineTypeAsObject_EN_US(String value) {
        if(value.equals("Chinese")) return CuisineType.CHINESE;
        else if(value.equals("Cantonese")) return CuisineType.CANTONESE;
        else if(value.equals("Hainanese")) return CuisineType.HAINANESE;
        else if(value.equals("Hakka")) return CuisineType.HAKKA;
        else if(value.equals("Hokkien")) return CuisineType.HOKKIEN;
        else if(value.equals("Fujian")) return CuisineType.FUJIAN;
        else if(value.equals("Shanghai")) return CuisineType.SHANGHAI;
        else if(value.equals("Teochew")) return CuisineType.TEOCHEW;
        else if(value.equals("Xinjiang")) return CuisineType.XINJIANG;
        else if(value.equals("Malay")) return CuisineType.MALAY;
        else if(value.equals("Indian")) return CuisineType.INDIAN;
        else if(value.equals("Western")) return CuisineType.WESTERN;
        else if(value.equals("Japanese")) return CuisineType.JAPANESE;
        else if(value.equals("Korean")) return CuisineType.KOREAN;
        else return null;
    }
    
    // singapore english (en_SG)
    private static Object getCuisineTypeAsObject_EN_SG(String value) {
        if(value.equals("Chinese")) return CuisineType.CHINESE;
        else if(value.equals("Cantonese")) return CuisineType.CANTONESE;
        else if(value.equals("Hainanese")) return CuisineType.HAINANESE;
        else if(value.equals("Hakka")) return CuisineType.HAKKA;
        else if(value.equals("Hokkien")) return CuisineType.HOKKIEN;
        else if(value.equals("Fujian")) return CuisineType.FUJIAN;
        else if(value.equals("Shanghai")) return CuisineType.SHANGHAI;
        else if(value.equals("Teochew")) return CuisineType.TEOCHEW;
        else if(value.equals("Xinjiang")) return CuisineType.XINJIANG;
        else if(value.equals("Malay")) return CuisineType.MALAY;
        else if(value.equals("Indian")) return CuisineType.INDIAN;
        else if(value.equals("Western")) return CuisineType.WESTERN;
        else if(value.equals("Japanese")) return CuisineType.JAPANESE;
        else if(value.equals("Korean")) return CuisineType.KOREAN;
        else return null;
    }
    
    // bahasa malaysia (ms_MY)
    private static Object getCuisineTypeAsObject_MS_MY(String value) {
        if(value.equals("Cina")) return CuisineType.CHINESE;
        else if(value.equals("Kantonis")) return CuisineType.CANTONESE;
        else if(value.equals("Hainan")) return CuisineType.HAINANESE;
        else if(value.equals("Hakka")) return CuisineType.HAKKA;
        else if(value.equals("Hokkien")) return CuisineType.HOKKIEN;
        else if(value.equals("Fujian")) return CuisineType.FUJIAN;
        else if(value.equals("Shanghai")) return CuisineType.SHANGHAI;
        else if(value.equals("Teochew")) return CuisineType.TEOCHEW;
        else if(value.equals("Xinjiang")) return CuisineType.XINJIANG;
        else if(value.equals("Melayu")) return CuisineType.MALAY;
        else if(value.equals("India")) return CuisineType.INDIAN;
        else if(value.equals("Western")) return CuisineType.WESTERN;
        else if(value.equals("Jepun")) return CuisineType.JAPANESE;
        else if(value.equals("Korea")) return CuisineType.KOREAN;
        else return null;
    }
    
    // simplified chinese (zh_CN)
    private static String getCuisineTypeAsString_ZH_CN(CuisineType cuisineType) {
        String localizedCuisineType = "";
        
        switch (cuisineType) {
            case CHINESE: localizedCuisineType = "中国菜" ; break;
            case CANTONESE: localizedCuisineType = "粤菜"; break;
            case HAINANESE: localizedCuisineType = "海南美食"; break;
            case HAKKA: localizedCuisineType = "客家菜"; break;
            case HOKKIEN: localizedCuisineType = "福建美食"; break;
            case FUJIAN: localizedCuisineType = "闽菜"; break;
            case SHANGHAI: localizedCuisineType = "上海菜"; break;
            case TEOCHEW: localizedCuisineType = "潮州美食"; break;
            case XINJIANG: localizedCuisineType = "新疆美食"; break;
            case MALAY: localizedCuisineType = "马来美食"; break;
            case INDIAN: localizedCuisineType = "印度美食"; break;
            case WESTERN: localizedCuisineType = "中西美食"; break;
            case JAPANESE: localizedCuisineType = "日本料理"; break;
            case KOREAN: localizedCuisineType = "韩国料理"; break;
        }
        return localizedCuisineType;
    }
    
    // default (en_US)
    private static String getCuisineTypeAsString_EN_US(CuisineType cuisineType) {
        String localizedCuisineType = "";
        
        switch (cuisineType) {
            case CHINESE: localizedCuisineType = "Chinese" ; break;
            case CANTONESE: localizedCuisineType = "Cantonese"; break;
            case HAINANESE: localizedCuisineType = "Hainanese"; break;
            case HAKKA: localizedCuisineType = "Hakka"; break;
            case HOKKIEN: localizedCuisineType = "Hokkien"; break;
            case FUJIAN: localizedCuisineType = "Fujian"; break;
            case SHANGHAI: localizedCuisineType = "Shanghai"; break;
            case TEOCHEW: localizedCuisineType = "Teochew"; break;
            case XINJIANG: localizedCuisineType = "Xinjiang"; break;
            case MALAY: localizedCuisineType = "Malay"; break;
            case INDIAN: localizedCuisineType = "Indian"; break;
            case WESTERN: localizedCuisineType = "Western"; break;
            case JAPANESE: localizedCuisineType = "Japanese"; break;
            case KOREAN: localizedCuisineType = "Korean"; break;
        }
        return localizedCuisineType;
    }
    
    // singapore english (en_SG)
    private static String getCuisineTypeAsString_EN_SG(CuisineType cuisineType) {
        String localizedCuisineType = "";
        
        switch (cuisineType) {
            case CHINESE: localizedCuisineType = "Chinese" ; break;
            case CANTONESE: localizedCuisineType = "Cantonese"; break;
            case HAINANESE: localizedCuisineType = "Hainanese"; break;
            case HAKKA: localizedCuisineType = "Hakka"; break;
            case HOKKIEN: localizedCuisineType = "Hokkien"; break;
            case FUJIAN: localizedCuisineType = "Fujian"; break;
            case SHANGHAI: localizedCuisineType = "Shanghai"; break;
            case TEOCHEW: localizedCuisineType = "Teochew"; break;
            case XINJIANG: localizedCuisineType = "Xinjiang"; break;
            case MALAY: localizedCuisineType = "Malay"; break;
            case INDIAN: localizedCuisineType = "Indian"; break;
            case WESTERN: localizedCuisineType = "Western"; break;
            case JAPANESE: localizedCuisineType = "Japanese"; break;
            case KOREAN: localizedCuisineType = "Korean"; break;
        }
        return localizedCuisineType;
    }
    
    // bahasa malaysia (ms_MY)
    private static String getCuisineTypeAsString_MS_MY(CuisineType cuisineType) {
        String localizedCuisineType = "";
        
        switch (cuisineType) {
            case CHINESE: localizedCuisineType = "Cina" ; break;
            case CANTONESE: localizedCuisineType = "Kantonis"; break;
            case HAINANESE: localizedCuisineType = "Hainan"; break;
            case HAKKA: localizedCuisineType = "Hakka"; break;
            case HOKKIEN: localizedCuisineType = "Hokkien"; break;
            case FUJIAN: localizedCuisineType = "Fujian"; break;
            case SHANGHAI: localizedCuisineType = "Shanghai"; break;
            case TEOCHEW: localizedCuisineType = "Teochew"; break;
            case XINJIANG: localizedCuisineType = "Xinjiang"; break;
            case MALAY: localizedCuisineType = "Melayu"; break;
            case INDIAN: localizedCuisineType = "India"; break;
            case WESTERN: localizedCuisineType = "Western"; break;
            case JAPANESE: localizedCuisineType = "Jepun"; break;
            case KOREAN: localizedCuisineType = "Korea"; break;
        }
        return localizedCuisineType;
    }
}